package com.platform.qujia.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.platform.qujia.constantEnum.integralTypeEnum;
import com.platform.qujia.dao.CustomerPointsRecordMapper;
import com.platform.qujia.dao.PosMachineMapper;
import com.platform.qujia.param.request.OrderPayInfo;
import com.platform.qujia.pojo.*;
import com.platform.qujia.pojo.wx.TopUpOrderCollect;
import com.platform.qujia.service.PosMachineService;
import com.platform.qujia.utils.redis.RedisCacheUtil;
import lombok.SneakyThrows;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service("posMachineService")
public class PosMachineServiceImpl implements PosMachineService {
    @Resource
    private PosMachineMapper posMachineMapper;

    @Resource
    private RedisCacheUtil redisCacheUtil;
    @Resource
    private CustomerPointsRecordMapper customerPointsRecordMapper;

    @Override
    public SysUser selectUserByUserId(String userId) {
        return posMachineMapper.selectUserByUserId(userId);
    }

    @Override
    public SysUser selectUserByUserName(String userName) {
        return posMachineMapper.selectUserByUserName(userName);
    }

    @Override
    public List<SysUser> selectOilPersonnelIdByGun(SysUser gun) {
        return posMachineMapper.selectOilPersonnelIdByGun(gun);
    }



    //获取油站的油枪所绑定的打印设备

    @Override
    @Cacheable(value = { "PosMachineServiceImpl_getStationDeviceManager"},
            key="'s_' + #root.args[0].getStationId() + 'd_'+ #root.args[0].getDeviceNo()",
            cacheManager = "cacheManager1H",
            condition = "#root.args[0].getStationId() != null && #root.args[0].getDeviceNo() != null",
            unless = "#result == null"
    )
    public StationDeviceManager getStationDeviceManager(StationDeviceManager stationDeviceManager) {
        return posMachineMapper.getStationDeviceManager(stationDeviceManager);
    }



    public SysUser selectUserByUserIdCache(String userId){
        String key = "userid:" + userId;
        if(redisCacheUtil.hasKey(key)){
            return  redisCacheUtil.getCacheObject(key);
        }
        SysUser sysUser= posMachineMapper.selectUserByUserId(userId);
        redisCacheUtil.setCacheObject(key, sysUser,  7 , TimeUnit.HOURS);
        return sysUser;
    }

    @Override
    public List<SysRole> selectUserRoleByUserId(String userId) {
        return posMachineMapper.selectUserRoleByUserId(userId);
    }

    @Override
    public List<SysRole> selectUserRoleByUserIdByPos(String userId,Integer stationId){
        return posMachineMapper.selectUserRoleByUserIdByPos(userId,stationId);
    }

    @Override
    public StationDeviceManager getStationDeviceManagerNoCache(StationDeviceManager stationDeviceManager) {
        return  posMachineMapper.getStationDeviceManager(stationDeviceManager);
    }

    @Override
    public List<OrderPayInfo> getTopUpOrderList(Station station) {
        return posMachineMapper.getTopUpOrderList(station);
    }

    @Override
    public TopUpOrderCollect getRechargeRecordCollect(Station station) {
        return posMachineMapper.getRechargeRecordCollect(station);
    }
}
